package com.lgkj.ui.manager;

import android.app.ActivityOptions;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.HairSalonAdapter;
import com.lgkj.adapter.HairStyleAdapter;
import com.lgkj.bean.HairStyleBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.ui.ImageBrowseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/6/26 0026.
 */

public class StylistActivity extends BaseActivity {


    @BindView(R.id.gv_item)
    GridView gvItem;
    private HairStyleAdapter adapter;
    private HairStyleBean dataBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_style_list;
    }

    @Override
    public void initView() {
        setTitle("造型师");
    }

    @Override
    protected void onResume() {
        super.onResume();
        initSouce();
    }

    @Override
    public void initData() {
        adapter = new HairStyleAdapter(StylistActivity.this);
        gvItem.setAdapter(adapter);
        gvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.i(TAG, "onItemClick: " + i);
                if (i == (adapterView.getChildCount() - 1)) {
                    startActivity(new Intent(StylistActivity.this, StylistAddActivity.class));
                    //设置layout在PopupWindow中显示的位置
                    //添加
                } else {
                    Intent intent = new Intent(StylistActivity.this, ImageBrowseActivity.class);
                    intent.putExtra("image_url", adapter.getItem(i).getBarber_img());
                    startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(StylistActivity.this, gvItem, "shareNames").toBundle());
                }
            }
        });
        adapter.setClick(new HairSalonAdapter.MyClickListener() {
            @Override
            public void clickListener(View v) {
                int id = Integer.parseInt(v.getTag().toString());
                dele(adapter.getItem(id).getId());
            }
        });
    }

    private void initSouce() {
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(StylistActivity.this));
        map.put("barbershop_id", CommonUtils.getShopId(StylistActivity.this));
        HttpUtils.postHttp(map, CommonConstant.barberList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    dataBean = mGson.fromJson(String.valueOf(jsonObject), HairStyleBean.class);
                    if (dataBean != null) {
                        adapter.setData(dataBean.getData());
                        adapter.notifyDataSetChanged();
                    }
                }

            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    private void dele(String id) {
        showDiaLog("正在删除");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(StylistActivity.this));
        map.put("id", id);
        HttpUtils.postHttp(map, CommonConstant.delBarber, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    initSouce();
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }
}
